#include "Timer6.h"




void TIM6_Config(uint16_t Period, uint16_t Prescaler)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);	//挂载在APB1总线上
	TIM_TimeBaseInitTypeDef TIM_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	TIM_InitStructure.TIM_Period = Period;
	TIM_InitStructure.TIM_Prescaler = Prescaler;
	TIM_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;	//基本定时器只支持向上计数
	TIM_InitStructure.TIM_RepetitionCounter = DISABLE;	//重复计数次数，基本定时器也不支持
	TIM_TimeBaseInit(TIM6, &TIM_InitStructure);
	
	TIM_ClearITPendingBit (TIM6, TIM_IT_Update);//先清除中断标志位
	TIM_ITConfig(TIM6, TIM_IT_Update | TIM_IT_Trigger, ENABLE);	//使能
	TIM_Cmd(TIM6, ENABLE);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
	NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
}

void TIM6_IRQHandler(void)//500ms基准定时
{
	static uint8_t LED_Count;
	if (TIM_GetITStatus (TIM6, TIM_IT_Update) == SET )
	{
		//如果读到0，给1，否则给0,实现500ms翻转电平
		( GPIO_ReadOutputDataBit(LED0_PORT, LED0) == RESET ) 		\
		? GPIO_SetBits(LED0_PORT, LED0)								\
		: GPIO_ResetBits(LED0_PORT, LED0);	//三目运算符，更简洁
		
		LED_Count++;
		if (LED_Count >= 2) //两次计数为1s
		{
			( GPIO_ReadOutputDataBit(LED1_PORT, LED1) == RESET )	\
			? GPIO_SetBits(LED1_PORT, LED1) 						\
			: GPIO_ResetBits(LED1_PORT, LED1);
			LED_Count = 0;
		}
		TIM_ClearITPendingBit (TIM6, TIM_IT_Update);	//清除中断标志位
	}
}
